package com.danskin.wechat.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.cache.JedisClient;
import com.danskin.wechat.entity.AccountLog;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.log.dao.AccountLogDao;
import com.danskin.wechat.model.LoginUser;
import com.danskin.wechat.service.WxMediaPlatformService;
import com.danskin.wechat.util.JsonUtils;

public class BaseServiceImpl {

	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private AccountLogDao accountLogDao;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private WxMediaPlatformService mpService;

	@Value("${STANDAR_AREA_MAP}")
	public String STANDAR_AREA_MAP;
	@Value("${MDMCODE_STANDAER_MAP}")
	public String MDMCODE_STANDAER_MAP;

	Map<String, String> SysAreaMap = new HashMap<String, String>();
	Map<String, String> MDMCodeStandarCodeMap = new HashMap<String, String>();

	void add(String opreateContent, String opreateBlank) {
		LoginUser attribute = (LoginUser) request.getAttribute("loginUser");
		if (null != attribute) {
			AccountLog log = new AccountLog();
			if (attribute.getMpId() > 0) {
				WxMediaPlatform wxMediaPlatform = mpService.getWxMediaPlatformById(attribute.getMpId());
				log.setNickName(wxMediaPlatform.getNickName());
				log.setMpId(attribute.getMpId());
			}
			log.setOpreateBlank(opreateBlank);
			log.setOpreateContent(opreateContent);
			log.setUserId(attribute.getUserId());
			log.setUserName(attribute.getUserName());
			log.setOpreateIp(attribute.getIp());
			log.setTableName("log_" + new SimpleDateFormat("yyyyMM").format(new Date()));
			log.setOpreateTime(new Date());
			int i = accountLogDao.insertSelective(log);
		}

	}

//	void addForApi(int mpId, String opreateContent, String opreateBlank) {
//
//		WxMediaPlatform wxMediaPlatform = mpService.getWxMediaPlatformById(mpId);
//		if (wxMediaPlatform != null) {
//
//			AccountLog log = new AccountLog();
//			log.setNickName(wxMediaPlatform.getNickName());
//			log.setMpId(mpId);
//			log.setOpreateBlank(opreateBlank);
//			log.setOpreateContent(opreateContent);
//			log.setUserId(0);
//			log.setUserName("API");
//			log.setOpreateIp("0.0.0.0");
//			log.setTableName("log_" + new SimpleDateFormat("yyyyMM").format(new Date()));
//			log.setOpreateTime(new Date());
//			int i = accountLogDao.insertSelective(log);
//		}
//
//	}

	public Map<String, String> getMDMCodeAndStandarCodeMap() {
		try {
			String json = jedisClient.get(MDMCODE_STANDAER_MAP);
			if (!StringUtils.isBlank(json)) {
				// JSON转换为pojo
				MDMCodeStandarCodeMap = JsonUtils.jsonToPojo(json, Map.class);
				return MDMCodeStandarCodeMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Map<String, String> getSysAreaMap() {
		try {
			String json = jedisClient.get(STANDAR_AREA_MAP);
			if (!StringUtils.isBlank(json)) {
				// JSON转换为pojo
				SysAreaMap = JsonUtils.jsonToPojo(json, Map.class);
				return SysAreaMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected String getJSONValue(String key, JSONObject json) {
		if (json.containsKey(key)) {
			Object obj = json.get(key);
			if (obj != null) {
				return json.get(key).toString();
			}
		}
		return null;
	}
}
